<%-- 
    Document   : template
    Created on : 2016-3-10, 17:22:23
    Author     : Administrator
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@include file="../link.jsp" %>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title><%=DataConfig.getInstance().getConfig().get("systemName")%></title>
        <link rel="icon" href="${base}/favicon.ico" type="image/x-icon">
        <link href="${base}/css/bootstrap-paper.css" rel="stylesheet">
        <link href="${base}/css/font-awesome.css" rel="stylesheet">
        <link href="${base}/css/layui.css" rel="stylesheet">
        <link href="${base}/css/nprogress.css" rel="stylesheet">
<!--        <link href="${base}/css/datepicker3.css" rel="stylesheet">-->

        <script type="text/javascript" src="${base}/js/jquery-1.9.1.js"></script>
        <script type="text/javascript" src="${base}/js/bootstrap.min.js"></script>
        <script type="text/javascript" src="${base}/js/angular/angular.min.js"></script>
        <script type="text/javascript" src="${base}/js/jquery.cxhd.js"></script>
        <script type="text/javascript" src="${base}/js/layui.all.js"></script>
         <script type="text/javascript" src="${base}/js/nprogress.js"></script>
        
<!--        <script type="text/javascript" src="${base}/js/bootstrap-datepicker.js"></script>
        <script type="text/javascript" src="${base}/js/bootstrap-datepicker.zh-CN.js"></script>-->

        <script src="${base}/js/angular/ui-bootstrap-tpls-0.12.0.min.js"></script>
        <script type="text/javascript" src="${base}/js/angular-base.js"></script>
    </head>
    <style>
        .mypage > .pagination{
            margin: 0;
        }
        .panel-footer{
            padding: 0;
        }
        .panel{
            margin: 0;
        }
        #patient_list>th{
            white-space: nowrap;
            border-top: none;
        }
        .table-striped >td{
            border-top: 1px  #999999 dashed 
        }
        .addStatus{
            border: 2px #006621 solid;
        }

        /*---滚动条默认显示样式--*/
        ::-webkit-scrollbar-thumb{
            background-color:#018EE8;
            height:50px;
            outline-offset:-2px;
            outline:2px solid #fff;
            -webkit-border-radius:4px;
            border: 2px solid #fff;
        }
        /*---鼠标点击滚动条显示样式--*/
        ::-webkit-scrollbar-thumb:hover{
            background-color:#FB4446;
            height:50px;
            -webkit-border-radius:4px;
        }
        /*---滚动条大小--*/
        ::-webkit-scrollbar{
            width:8px;
            height:8px;
        }
        /*---滚动框背景样式--*/
        ::-webkit-scrollbar-track-piece{
            background-color:#fff;
            -webkit-border-radius:0;
        }

    </style>
    <body ng-app="hostList" ng-controller="mainCtrl" style="font-family: 微软雅黑">


        <div class="panel panel-default" style="position: fixed;top: 0px;width: 100%">
            <div class="panel-heading">
                <span style="float: left">信息列表</span>
                <div>
                    <input ng-keyup="enterKey($event)" ng-model="searchContent" placeholder="名字/病人ID" style="width:320px; float: left;margin-left: 20px" class="form-control input-sm" />
                    <button ng-click="getList()" style="float: left" class="btn btn-success btn-sm"><span class="fa fa-search"></span>&nbsp;&nbsp;搜索</button>
                    <!--<button ng-click="showCreate()" style="float: left;margin-left: 20px;" class="btn btn-default btn-sm"><span class="fa fa-user"></span>&nbsp;&nbsp;创建病人</button> &nbsp;&nbsp;&nbsp;&nbsp;-->


                    <button ng-click="getList()" style="float: left;margin-left: 20px;" class="btn btn-success btn-sm"><span class="fa fa-refresh"></span>&nbsp;&nbsp;刷新</button> &nbsp;&nbsp;&nbsp;&nbsp;
                    <select ng-show="hospitalFlag" class="form-control input-sm" ng-change="getList()" ng-model="hospitalCode"  style="float: left;margin-left: 20px; width: 160px;" >
                        <option selected="selected" >---请选择医院---</option>
                        <option ng-repeat="hos in hospitals" value="{{hos.hospital_code}}" >{{hos.name}}</option>
                    </select>
                    <input    ng-model="start_date" id="start_date"  placeholder="开始时间" data-date-format="yyyy-mm-dd" type="text" style="width:220px; float: left;margin-left: 20px" class="form-control input-sm" />
                    <div style="float: left;margin-left: 20px;margin-right: 20px">到</div>
                    <input ng-model="end_date" min-v ="{{start_date}}" id="end_date" data-date-format="yyyy-mm-dd" placeholder="结束时间" type="text" style="width:220px; float: left;margin-left: 20px" class="form-control input-sm" />


                </div>
                <div style="clear: both"></div>
            </div>
        </div>

        <table  class="autoHeight" style="width: 100%;position: fixed;top: 50px; bottom: 50px">
            <tr>
                <td class="x-left" style="width: 100%;padding: 0px;overflow: auto;border-right: 1px #cccccc solid" valign="top">
                    <div class="autoHeight">
                        <table id="patient_list" style="border-top: none" class="table table-hover  table-striped ">
                            <tr>
                                <th style="width:80px;">工具</th>
                                <th style="width:120px;" >病历号&nbsp;&nbsp;&nbsp;&nbsp;<span ng-click="hideRight()" style="line-height: 20px;color: red;cursor: pointer;float: right;font-size: 16px;font-weight: bold" class=" show-right fa fa-angle-double-right"></span>&nbsp;&nbsp;&nbsp;</th>
                                <th style="width:80px;">名字</th>
                                <th class="x-hide">性别</th>
                                <th class="x-hide">年龄</th>
                                <th class="x-hide">手机号</th>
                                <th class="x-hide">事件名称</th>
                                <th class="x-hide">时间</th>
                                <th class="x-hide">医院名称</th>

                            </tr>
                            <tr ng-repeat="list in lists" ng-show="lists" >
                                <td>
                                    <!--<span class="label label-default">导入</span>-->
                                    <button ng-click="importDetail(list)" class="btn btn-default btn-xs"> <span class="glyphicon glyphicon-import" title="导入"></span></button>
                                    <button ng-show='list.phone'  class="btn btn-success btn-xs" data-toggle="popover" data-toggle="popover" data-trigger="focus" title="扫我打电话" data-html="true" data-content="<img src='getQrCode?data={{list.phone}}' />" > 
                                        <span   class="fa fa-phone" ></span></button>                                   

                                </td>
                                <td ng-click='previewDetail(list)' ng-bind="list.host_id"  ng-style="list.tdBackground"></td>
                                <td ng-click='previewDetail(list)' ><span ng-bind="list.name"></span><span style="color: #FB4446;font-size: 8px;" class="fa fa-star" ng-show="list.last_event_name"></span></td>
                                <td class="x-hide" ng-bind="list.sex" ></td>
                                <td ng-dblclick="deleteByPk(list.pk)" ng-bind="list.age" class="x-hide"></td>
                                <td class="x-hide" ng-bind="list.phone"></td>
                                <td class="x-hide" ng-bind="list.event_name"></td>
                                <td class="x-hide" ng-bind="list.event_time"></td>
                                <td class="x-hide" ng-bind="list.hos_name"></td>
                            </tr>
                        </table>
                    </div>
                </td>

                <td class=""></td>
                <td class="x-right" valign="top">
                    <iframe src="" id="iframepage" name="iframepage" frameBorder=0 scrolling=auto width="100%"  ></iframe>
                </td>
            </tr>
        </table>
        <div class="panel panel-default" style="position: fixed;bottom: 0px;width: 100%">
            <div class="panel panel-footer">
                <div class="mypage" style="width: 100%;text-align: center">
                    <pagination ng-change="pageChanged()" previous-text="上一页" next-text="下一页" first-text="第一页" last-text="最后一页" ng-model="data.currentPage" items-per-page="data.pageSize" total-items="data.bigTotalItems" ng-model="data.bigCurrentPage" max-size="data.maxSize" class="pagination-sm" boundary-links="true" rotate="false" num-pages="numPages"></pagination>

                </div>

            </div>
        </div>


        <div class="modal fade mode-x" role="dialog" aria-labelledby="gridSystemModalLabel">
            <div class="modal-dialog" role="document">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                        <h4 class="modal-title" id="gridSystemModalLabel">新病人</h4>
                    </div>
                    <div class="modal-body">
                        <form  name="users" id="users">
                            <table class="table">
                                <tr>
                                    <td>姓名</td>
                                    <td>
                                        <input ng-model="user.name" type="text" class="form-control">
                                    </td>
                                    <td>性别</td>
                                    <td>
                                        <input ng-model="user.sex" value="F" type="radio" > 女
                                        <input ng-model="user.sex" value="M" type="radio" > 男
                                    </td>
                                </tr>
                                <tr>
                                    <td>年龄</td>
                                    <td>
                                        <input ng-model="user.age" type="text" class="form-control">
                                    </td>
                                    <td>生日</td>
                                    <td>
                                        <input id="datePick" ng-model="user.birth" type="text" class="form-control">
                                    </td>
                                </tr>
                                <tr>
                                    <td>手机号</td>
                                    <td>
                                        <input ng-model="user.phone" type="text" class="form-control">
                                    </td>
                                    <td>身份证</td>
                                    <td>
                                        <input ng-model="user.card_no" type="text" class="form-control">
                                    </td>
                                </tr>
                                <tr>
                                    <td>地址</td>
                                    <td colspan="3">
                                        <input ng-model="user.address" type="text" class="form-control">
                                    </td>

                                </tr>
                                <tr>
                                    <td>医院</td>
                                    <td colspan="3">
                                        <select ng-model="user.hospital_code">
                                            <option ng-repeat="hos in hospitals" value="{{hos.hospital_code}}" >{{hos.name}}</option>
                                        </select>
                                    </td>

                                </tr>
                            </table>
                        </form>

                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                        <button  ng-click="saveUser()" ng-disabled="users.$invalid" type="button" class="btn btn-primary"><span class="fa fa-save"></span>&nbsp;&nbsp;保存</button>
                    </div>
                </div><!-- /.modal-content -->
            </div><!-- /.modal-dialog -->
        </div><!-- /.modal -->


        <div class="modal fade bs-example-modal-lg show_big_image" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel">
            <div class="modal-dialog modal-lg">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                        <h4 class="modal-title" id="gridSystemModalLabel">导入{{currentName}}的病历记录</h4>

                    </div>
                    <div class="modal-body" style="max-height: 400px;overflow: auto">
                        <div ng-repeat="image in images" class="col-xs-12 col-lg-4">
                            <input style="margin-bottom: 5px;" placeholder="事件名称" type="text" class="form-control" ng-model="image.event_description">
                            <a  ng-click="addData(image, $event)"  ng-dblclick="deleteItem(image, $event)"  href="#" class="thumbnail">
                                <img  ng-src="{{image.url}}" alt="">
                            </a>
                        </div>
                        <div style="clear: both;"></div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                        <button  ng-click="saveDetail()" type="button" class="btn btn-primary"><span class="fa fa-save"></span>&nbsp;&nbsp;导入</button>
                    </div>
                </div>
            </div>
        </div>


    </body>
</html>
<script type="text/javascript">
    console.log(window.parent);
    if (!window.parent) {
        location = "/MedicalInfo/main";
    }
    $(function () {
        $("[data-toggle='tooltip']").tooltip();
    });
    var itemDetail;
    angular.module("hostList", ['ui.bootstrap','baseModule'])

            .controller("mainCtrl", function ($scope,base) {
                var p = '<%=request.getAttribute("p")%>';
                var so = $scope;
                $scope.dataFlag = true;
                $scope.hospitalFlag = true;
                var hospital_info = userInfo.hospital_code;
                if (!hospital_info&&!hospital_info.hospital_code) {

                    $scope.dataFlag = false;

                } else if (config.centerHospitalCode != hospital_info.hospital_code) {
                     console.log(hospital_info.hospital_code);
                    so.hospitalCode = hospital_info.hospital_code;
                    $scope.hospitalFlag = false;
                }
                else{
                   
                }

                if (p && p != 'null') {
                    $scope.searchContent = p;
                }
               
                $scope.data = {currentPage: 1};
                $scope.images = [];
                so.setPage = function (pageNo) {
                    $scope.data.currentPage = pageNo;
                };
                so.items = ['入院记录', '体温单', '病程记录', "知情同意书"];
                so.pageChanged = function () {
                    so.getList();
                };

                so.data.maxSize = 10;
                so.data.bigTotalItems = 20;
                so.data.bigCurrentPage = 1;
                so.data.pageSize = 40;


                //$(".datePick").datepicker({format: 'yyyy-mm-dd'});


                so.getHospital_code = function () {

                    $.post("${base}/patient/getHospital", function (data) {
                        if (data) {
                            $scope.hospitals = data;
                            if ($scope.hospitals) {
                                $scope.hospitals.push({name: "所有医院"});
                            }
                            console.log($scope.hospitals);
                            $scope.$apply();
                        }
                    })

                }

                so.getList = function () {
                    if(!$scope.dataFlag){
                        return;
                    }
                    base.post("${base}/patient/getHostListByEcg", {content: so.searchContent, pageSize: so.data.pageSize, currentPage: so.data.currentPage, hospital: so.hospitalCode, start_date: $scope.start_date, end_date: $scope.end_date}, function (data) {
                        if (data) {
                            so.lists = data.list;
                            so.data.bigTotalItems = data.totalRow;
                            if (so.lists) {
                                for (var i in so.lists) {
                                    if (so.lists[i].sex == '男') {
                                        so.lists[i].tdBackground = {"background-color": "#9999ff"};
                                    } else {
                                        so.lists[i].tdBackground = {"background-color": "#ffcccc"};
                                    }
                                    if (!so.lists[i].age) {
                                        so.lists[i].age = GetAgeByBrithday(so.lists[i].birth);
                                        // so.lists[i].age = so.lists[i].birth;
                                    }
                                }
                            }
                           // so.$apply();
                            $(".x-hide").show();
                            $(".show-right").hide();
                            $(".x-left").animate({"width": "100%"});
                            $("[data-toggle='popover']").popover();
                        }
                    });
                }
                so.hideRight = function () {
                    $(".x-hide").show();
                    $(".show-right").hide();
                    $(".x-left").animate({"width": "100%"});
                }
                so.importDetail = function (item) {
                    so.currentName = item.name;
                    so.currentHost_id = item.host_id;
                    $(".show_big_image").modal("show");
                }
                so.deleteByPk = function (pk) {
                    steveConfrim("确定删除吗?", function () {
                        $.post("${base}/patient/deletePatient", {pk: pk}, function (data) {
                            if (data == 'true') {
                                steveAl("删除成功!", 1000);
                            } else {
                                steveAl("删除失败!", 1000);
                            }
                        })
                    })

                }

                so.addData = function (item, event) {
                    var $this = event.target;
                    if ($($this).hasClass("addStatus")) {
                        $($this).removeClass("addStatus");
                        item.flag = false;
                    } else {
                        $($this).addClass("addStatus");
                        item.flag = true;
                    }
                }

                so.deleteItem = function (item, event) {
                    steveConfrim("确定删除吗?", function () {
                        if (web) {
                            web.send(JSON.stringify({"type": "deleteFile", 'data': item.fileId}));
                            for (var i in  $scope.images) {
                                if ($scope.images[i].fileId == item.fileId) {
                                    $scope.images.splice(i, 1);
                                    break;
                                }
                            }
                            $scope.$apply();
                        }
                    })

                }
                so.showCreate = function () {
                    $(".mode-x").modal('show');
                    if (!so.hospitals) {
                        so.getHospital_code();
                    }
                }
                so.enterKey = function (event) {
                    if (event.keyCode == 13) {
                        so.getList();
                    }
                }
                so.saveUser = function () {
                    $.post("${base}/patient/getHost", $scope.user, function (data) {
                        if (data && data != 'null') {
                            steveAl("创建成功", 1000);
                            so.getList();
                            setTimeout(function () {
                                $(".mode-x").modal("hide");
                            }, 1000);
                        } else {
                            steveAl("创建失败", 1000);
                        }
                    });
                }
                so.previewDetail = function (item) {
                    if (window.parent) {
                        window.parent.addNewTab({href: "${base}/patient/openDetail?host_id=" + item.host_id,
//                            icon: icon,
                            title: item.name + "的资料"})
                    } else {
                        $(".x-hide").hide();
                        $(".show-right").show();
                        $(".x-left").animate({"width": "280px"});
                        itemDetail = item;
                        $("#iframepage").attr("src", "patient_detail.jsp");
                    }

                };
                var web;
                var login = function () {
                    try {
                        web = new WebSocket("ws://127.0.0.1:19000/websocket");

                    } catch (e) {

                    }
                    web.onopen = function () {
                        console.log("open");
                        // alert(JSON.stringify(room));
                        // web.send(JSON.stringify(room));
                    }


                    web.onmessage = function (e) {


                        parseMessage(JSON.parse(e.data));

                    };

                    web.onclose = function (e) {
//                        setTimeout(function () {
//                            console.log("重连服务器");
//                            try {
//                               // login();
//                            } catch (e) {
//                            }
//                        }, 5000);
                    };
                    web.onerror = function (e) {

                    };
                }

                var parseMessage = function (data) {
                    if (data) {
                        if ($scope.images.length == 0) {
                            $scope.images = data;
                        } else {
                            for (t in data) {
                                var flag = true;
                                for (y in $scope.images) {
                                    if (data[t].fileId == $scope.images[y].fileId) {
                                        flag = false;
                                    }
                                }
                                if (flag) {
                                    $scope.images.push(data[t]);
                                }
                            }
                        }
                        // $scope.images = data;
                    }
                    if ($scope.images) {
                        for (i in  $scope.images) {
                            $scope.images[i].url = "getImage?fileId=" + $scope.images[i].fileId;

                        }
                        console.log($scope.images);
                        $scope.$apply();
                    }


                }
                so.saveDetail = function () {
                    so.saveData = [];
                    var fileIds = [];

                    for (i in  $scope.images) {
                        if ($scope.images[i].flag) {
                            fileIds.push($scope.images[i].fileId);
                            so.saveData.push({'department': "住院部", 'event_name': '病历', 'event_description': $scope.images[i].event_description, 'hospital_name': "", 'result_type': 'image', 'event_result': $scope.images[i].fileId});
                        }
                    }
                    console.log(so.saveData);
                    if (so.saveData.length > 0) {
                        $.post("${base}/patient/saveLotInfo", {host_id: so.currentHost_id, data: JSON.stringify(so.saveData)}, function (data) {

                            if (data == 'true') {
                                steveAl("导入成功", 1000);
                                $(".show_big_image").modal("hide");
                                if (web) {
                                    web.send(JSON.stringify({"type": "deleteFile", 'data': fileIds.join(",")}));
                                    $scope.images = [];
                                    $scope.$apply();
                                }
                            } else {
                                steveAl("导入失败", 1000);
                            }
                        });
                    } else {
                        steveAl("请先选中文件");
                    }
                }
                //login();
                layui.use('laydate', function () {
                    var laydate = layui.laydate;

                    var start = {
//    min: laydate.now(),
                        max: '2099-06-16 23:59:59'
                        , istoday: false
                        , choose: function (datas) {
                            $scope.start_date = datas;
                            end.min = datas; //开始日选好后，重置结束日的最小日期
                            end.start = datas; //将结束日的初始值设定为开始日
                        }
                    };

                    var end = {
                        min: laydate.now()
                        , max: '2099-06-16 23:59:59'
                        , istoday: false
                        , choose: function (datas) {
                            $scope.end_date = datas;
                            start.max = datas; //结束日选好后，重置开始日的最大日期
                        }
                    };

                    document.getElementById('start_date').onclick = function () {
                        start.elem = this;
                        laydate(start);
                    }
                    document.getElementById('end_date').onclick = function () {
                        end.elem = this
                        laydate(end);
                    }

                });


                so.getList();
                so.getHospital_code();

            });

    function getDetail() {
        console.log(itemDetail);
        return itemDetail;

    }
    function autoHeight() {
        var bHeight = $(document).height();
        $(".autoHeight").height(bHeight - 100);
        $("#iframepage").height(bHeight - 60);
    }
    autoHeight();
    reinitIframe();
    function reinitIframe() {
        //  var iframe = document.getElementById("iframepage");
        try {
            var bHeight = $(document).height();
            $("#ifm").height(bHeight);

            $("#iframepage").height($(".x-left").height());
        } catch (ex) {
        }
    }

    $(window).resize(function () {
        autoHeight();
    });
    tools.delCookie("detail_pk");



</script>
